package com.review.Test.ConcurrentUtil;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class UsecCyclicBarrier {
	
	public static void main(String[] args) {
		ExecutorService pool = Executors.newFixedThreadPool(4);
		CyclicBarrier cyclicBarrier = new CyclicBarrier(4);
		
		Sporter s1 = new Sporter("张蓓萌", cyclicBarrier);
		Sporter s2 = new Sporter("苏炳添", cyclicBarrier);
		Sporter s3 = new Sporter("孙雪健", cyclicBarrier);
		Sporter s4 = new Sporter("谢震业", cyclicBarrier);
		
		pool.execute(s1);
		pool.execute(s2);
		pool.execute(s3);
		pool.execute(s4);
		
		pool.shutdown();
		
	}

}

class Sporter implements Runnable{
	private String name;
	private CyclicBarrier cyclicBarrier;
	public Sporter(String name, CyclicBarrier cyclicBarrier) {
		super();
		this.name = name;
		this.cyclicBarrier = cyclicBarrier;
	}
	@Override
	public void run() {
		try {
			Thread.sleep(1000 * new Random().nextInt(5));
			System.out.println(name+ "   预备。。。");
			cyclicBarrier.await();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(name+ "   跑。。。");
		
	}
	
}
